@extends('admin.layout.base')
@section('title', '编辑商品')
@section('pluginstyle')
<link rel="stylesheet" type="text/css" href="{{ asset('/plugins/webuploader/webuploader.css') }}">
@endsection
@section('content')
<div class="content-wrapper">
    <!-- Content Header (Page header) -->
    <section class="content-header">
        <h1>
            修改商品
        </h1>
        <ol class="breadcrumb">
            <li><a href="{{ url('/admin') }}"><i class="fa fa-dashboard"></i> 首页</a></li>
            <li><a href="{{ url('/admin/goods') }}">商品管理</a></li>
            <li class="active">修改商品</li>
        </ol>
    </section>

    <!-- Main content -->
    <section class="content">
        <div class="row">
            <!-- right column -->
            <div class="col-md-12">
                <!-- Horizontal Form -->
                <div class="box box-info">
                    <div class="box-header with-border">
                        <h3 class="box-title">修改商品</h3>
                    </div>
                    <!-- /.box-header -->
                    @if(session('errors'))
                    <div class="alert alert-danger alert-dismissible">
                        <button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
                        @foreach($errors->all() as $key => $error)
                        {{ $key+1 . ':' . $error }}
                        @endforeach
                    </div>
                    @endif

                    <div class="box-body">
                        <div class="nav-tabs-custom">
                            <ul class="nav nav-tabs">
                                <li class="active"><a href="#tab_1" data-toggle="tab" aria-expanded="true">基本信息</a></li>
                                <li class=""><a href="#tab_2" data-toggle="tab" aria-expanded="false">商品属性</a></li>
                            </ul>

                            <div class="tab-content">
                                <div class="tab-pane active" id="tab_1">
                                    <form class="form-horizontal" action="{{ url('/admin/goods',['id'=>$goods->id]) }}" method="post" enctype="multipart/form-data">
                                        {!! csrf_field() !!}
                                        {{ method_field('PUT') }}
                                        <div class="form-group">
                                            <label for="goods_name" class="col-sm-2 control-label">商品名称</label>
                                            <div class="col-sm-5">
                                                <input type="text" class="form-control" id="goods_name" name="goods_name" value="{{ $goods->goods_name }}">
                                            </div>
                                        </div>
                                        <div class="form-group">
                                            <label for="cate_id" class="col-sm-2 control-label">所属分类</label>
                                            <div class="col-sm-5">
                                                <select name="cate_id" id="cate_id" class="form-control">
                                                    <option value="">-- 请选择分类 --</option>
                                                    @foreach($category as $key => $cate)
                                                    @if($key == 0)
                                                    <optgroup label="{{ $cate['cate_name'] }}">
                                                        @elseif($cate['level'] == 0)
                                                    </optgroup><optgroup label="{{ $cate['cate_name'] }}">
                                                        @else
                                                        <option value="{{ $cate['id'] }}" {{ $goods->cate_id == $cate['id'] ? 'selected' : '' }}>{{ str_repeat('&nbsp;&nbsp;', $cate['level']*2) }}{{ $cate['cate_name'] }}</option>
                                                        @endif
                                                        @endforeach
                                                    </optgroup>
                                                </select>
                                            </div>
                                        </div>
                                        <div class="form-group">
                                            <label for="goods_price" class="col-sm-2 control-label">商品价格</label>
                                            <div class="col-sm-3">
                                                <input type="text" class="form-control" id="goods_price" name="goods_price" value="{{ $goods->goods_price }}">
                                            </div>
                                        </div>
                                        <div class="form-group">
                                            <label for="goods_brief" class="col-sm-2 control-label">商品简述</label>
                                            <div class="col-sm-7">
                                                <input type="text" class="form-control" id="goods_brief" name="goods_brief" value="{{ $goods->goods_brief }}">
                                            </div>
                                        </div>
                                        <div class="form-group">
                                            <label for="goods_img" class="col-sm-2 control-label">重设图片</label>
                                            <div class="col-sm-5">
                                                <div id="goods">选择图片</div>
                                                <img src="{{ $goods->goods_thumb }}" alt="" class="img-thumbnail" />
                                                <input type="hidden" class="form-control" id="goods_img" name="goods_img" value="{{ $goods->goods_img }}">
                                                <input type="hidden" class="form-control" id="goods_thumb" name="goods_thumb" value="{{ $goods->goods_thumb }}">
                                            </div>
                                        </div>
                                        <div class="form-group">
                                            <label for="goods_spec" class="col-sm-2 control-label">商品规格参数</label>
                                            <div class="col-sm-10">
                                                <textarea name="goods_spec" id="goods_spec" class="" rows="10">{!! $goods->goods_spec !!}</textarea>
                                            </div>
                                        </div>
                                        <div class="form-group">
                                            <label for="goods_desc" class="col-sm-2 control-label">商品详情</label>
                                            <div class="col-sm-10">
                                                <textarea name="goods_desc" id="goods_desc" class="" rows="10">{!! $goods->goods_desc !!}</textarea>
                                            </div>
                                        </div>
                                        <div class="form-group">
                                            <div class="col-sm-10 col-sm-offset-2">
                                                <button type="submit" class="btn btn-primary">保存</button>
                                                <button type="button" class="btn btn-default pull-left" data-dismiss="modal">取消</button>
                                            </div>
                                        </div>
                                    </form>
                                </div>
                                <!-- /.tab-pane -->
                                <div class="tab-pane" id="tab_2">
                                    <button class="btn btn-info" id="attr-add" type="button" data-toggle="modal" data-target="#attr-add-box"><i class="fa fa-plus"></i>增加属性</button>
                                    <hr />
                                    @foreach($attrs as $attr)
                                    <form action="javascript:void(0);" class="form-inline" method="post">
                                        <input type="hidden" name="id" value="{{ $attr->id }}" />
                                        <div class="form-group">
                                            <label>属性名：</label>
                                            <input type="text" class="form-control" placeholder="属性名" name="attr_type" value="{{ $attr->attr_type }}">
                                        </div>
                                        <div class="form-group">
                                            <label>价格：</label>
                                            <input type="text" class="form-control" placeholder="属性名" name="attr_price" value="{{ $attr->attr_price }}">
                                        </div>
                                        <button type="button" class="btn btn-default attr-save">修改</button>
                                        <button type="button" class="btn btn-danger attr-remove">删除</button>
                                    </form>
                                    <br />
                                    @endforeach
                                </div>
                                <!-- /.tab-pane -->
                            </div>
                            <!-- /.tab-content -->
                        </div>
                    </div>
                    </form>
                    <!-- form start -->
                </div>
                <!-- /.box -->
            </div>
            <!--/.col (right) -->
        </div>
        <!-- /.row -->
    </section>
    <!-- /.content -->
</div>
<div class="modal fade" id="attr-add-box">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">×</span>
                </button>
                <h4 class="modal-title">增加属性</h4>
            </div>
            <form action="{{ url('/admin/attr') }}" class="form-horizontal" method="post">
                <div class="modal-body">
                    {{ csrf_field() }}
                    <input type="hidden" name="goods_id" value="{{ $goods->id }}" />
                    <div class="form-group">
                        <label for="attr_type" class="col-sm-offset-2 col-sm-2 text-right control-label">名称：</label>
                        <div class="col-sm-6">
                            <input type="text" name="attr_type" id="attr_type" class="form-control"/>
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="attr_price" class="col-sm-offset-2 col-sm-2 text-right control-label">价格：</label>
                        <div class="col-sm-6">
                            <input type="text" name="attr_price" id="attr_price" class="form-control"/>
                        </div>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default pull-left" data-dismiss="modal">取消</button>
                    <button type="submit" class="btn btn-primary">保存</button>
                </div>
            </form>
        </div>
        <!-- /.modal-content -->
    </div>
    <!-- /.modal-dialog -->
</div>
@endsection
@section('pluginscript')
<!-- FastClick -->
<script src="{{ asset('/dist/admin/plugins/fastclick/fastclick.js') }}"></script>
<script type="text/javascript" src="{{ asset('/plugins/webuploader/webuploader.js') }}"></script>
<script type="text/javascript" src="{{ asset('/common/js/upload.js') }}"></script>
<script type="text/javascript">
$(function() {
    $.ajaxSetup({
        headers: {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
        }
    });
    uploadone('#goods');
});
</script>
<script type="text/javascript" charset="utf-8" src="{{ asset('/plugins/ueditor/ueditor.config.js') }}"></script>
<script type="text/javascript" charset="utf-8" src="{{ asset('/plugins/ueditor/ueditor.all.min.js') }}"></script>
<script>
$(function() {
    // 实例化文本编辑器
    var content = UE.getEditor('goods_desc');
    var spec = UE.getEditor('goods_spec');

    // 删除属性
    $('.attr-remove').click(function() {
        var result = confirm('确认要删除？');
        var parent = $(this).parents('form');
        var id = parent.children('input[name=id]').val();

        if (result == false) {
            return false;
        }

        $.get("{{ url('/admin/attr/delete') }}", {id: id}, function(msg) {
            if (msg.status == 1) {
                parent.remove();
            } else {
                alert(msg.info);
            }
        });
    });

    // 修改属性
    $('.attr-save').click(function() {
        var data = $(this).parents('form').serialize();
        $.post("{{ url('/admin/attr/update') }}", data, function(msg) {
            alert(msg.info);
            if (msg.status != '1')
                location.reload();
        });
    });
});
</script>
@endsection